﻿(function () {
    var CanvasRenderer = function (canvasContext) {
        this.canvasContext = canvasContext;
    };

    CanvasRenderer.prototype.renderObjects = function (objects) {
        this.canvasContext.clearRect(0, 0, this.canvasContext.canvas.width, this.canvasContext.canvas.height);

        var blockSize = 10;
        var objectsCount = objects.length;

        for (var index = 0; index < objectsCount; index++) {
            var currentObject = objects[index];

            var currentObjectBodyLength = currentObject.getBody.length;

            for (var element = 0; element < currentObjectBodyLength; element++) {
                var currentBlock = currentObject.getBody[element];

                var oldFillStyle = context.fillStyle;

                this.canvasContext.fillStyle = currentBlock.color;

                var topLeftX = currentBlock.x * blockSize;
                var topLeftY = currentBlock.y * blockSize;

                this.canvasContext.beginPath();
                this.canvasContext.moveTo(topLeftX, topLeftY);
                this.canvasContext.lineTo(topLeftX + blockSize, topLeftY);
                this.canvasContext.lineTo(topLeftX + blockSize, topLeftY + blockSize);
                this.canvasContext.lineTo(topLeftX, topLeftY + blockSize);
                this.canvasContext.lineTo(topLeftX, topLeftY);
                this.canvasContext.fill();
                this.canvasContext.stroke();

                context.fillStyle = oldFillStyle;
            }
        }
    };

    tetris.CanvasRenderer = CanvasRenderer;
})();